package com.fable.utils;

import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by weishuai on 18/4/17.
 */
public class SmsUtil {

    private static final String account = "N00000023564";//替换为您的账户
    private static final String secret = "8f9f5fe0-4200-11e8-a90d-717111521221";//替换为您的api密码
    private static final String host = "http://apis.7moor.com";
    private static final Logger log = LoggerFactory.getLogger(SmsUtil.class);

    public static boolean sendMessage(String phones, String content) {
        if (StringUtil.isBlank(phones) || StringUtil.isBlank(content)) {
            log.error("参数为空:phones:{},content:{}", JSON.toJSON(phones), content);
            return false;
        }
        Map<String,String> postPram=new HashMap<>();
        postPram.put("num",phones);
        postPram.put("templateNum","2");
        postPram.put("var1",content);


        String time = getDateTime();
        String sig = md5(account + secret + time);
        //查询坐席状态接口
        String interfacePath = "/v20160818/sms/sendInterfaceTemplateSms/";
        String url = host + interfacePath + account + "?sig=" + sig;
        String auth = base64(account + ":" + time);

        HttpUtils util = new HttpUtils();
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Authorization", auth);

        //N00000023564_2  模板编号
        String response = util.doPostStr(url, JSON.toJSONString(postPram), header,null);
        log.info("发送短信结果,response:{}",response);
        return true;
    }


    public static void main(String[] args) {
        SmsUtil.sendMessage("17712169979,18758160314","1.阿里巴巴 2.百度 3.腾讯");
        String time = getDateTime();
        String sig = md5(account + secret + time);
        //查询坐席状态接口
        String interfacePath = "/v20160818/sms/getSmsTemplate/";
        String url = host + interfacePath + account + "?sig=" + sig;
        String auth = base64(account + ":" + time);

        HttpUtils util = new HttpUtils();
        Map<String, String> header = new HashMap<>();
        header.put("Accept", "application/json");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Authorization", auth);

        //N00000023564_2  模板编号
        String response = util.doPost(url, null, header);
        System.out.println(response);
    }

    public static String md5(String text) {
        return DigestUtils.md5Hex(text).toUpperCase();
    }

    public static String base64(String text) {
        byte[] b = text.getBytes();
        Base64 base64 = new Base64();
        b = base64.encode(b);
        String s = new String(b);
        return s;
    }

    public static String getDateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        return sdf.format(new Date());
    }
}
